package com.shopping.controller;


import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.shopping.po.User;
import com.shopping.po.UserInfor;
import com.shopping.service.UserInforService;

@Controller
public class UserInforController {
	
	@Autowired
	private UserInforService inforService;
	@RequestMapping("/insertuserInfor")
	public String insertuserInfor(HttpSession session,Model model) {
		User user = (User) session.getAttribute("user");
		String username = user.getUsername();
		model.addAttribute("username1",username);
		return "insertInfor";
	}
	@RequestMapping("/updateuserInfor")
	public String updateuserInfor(HttpSession session,Model model) {
		
		User user = (User) session.getAttribute("user");
		Integer user_id = user.getUser_id();
		UserInfor infor = inforService.selectInfor(user_id);
		model.addAttribute("infor1",infor);
		return "updateInfor";
	}
	
	@RequestMapping("/selectuserInfor")
	public String selectuserInfor(HttpSession session,Model model) {
		
		User user = (User) session.getAttribute("user");
		Integer user_id = user.getUser_id();
		UserInfor infor = inforService.selectInfor(user_id);
		model.addAttribute("infor",infor);
		return "userInfor";
	}
	
	@RequestMapping("/toinsertInfor")
	public String insertuserInfor(UserInfor infor,HttpSession session,Model model,HttpServletRequest request) {
		User user = (User) session.getAttribute("user");
		String username = user.getUsername();
		Integer user_id = user.getUser_id();
		UserInfor infor1 = inforService.selectInfor(user_id);
		if(infor1 == null) {
			String realname=infor.getRealname();
			String gender= infor.getGender();
			String ars = infor.getArs();
			String home = infor.getHome();
			inforService.insertInfor(realname,gender,ars,home,user_id);//user_id
			return "redirect:/selectuserInfor";
		}else {
			request.setAttribute("error2", "您已经填写过了，只需要修改即可");
			return "insertInfor";
		}
	}
	
	@RequestMapping("/toupdateInfor")
	public String updateuserInfor(UserInfor infor,HttpSession session,Model model) {
		User user = (User) session.getAttribute("user");
		Integer user_id = user.getUser_id();
		String gender= infor.getGender();
		String ars = infor.getArs();
		String home = infor.getHome();
		inforService.updateInfor(gender,ars,home);//user_id
		return "redirect:/selectuserInfor";
	}

}
